﻿using System;

public class Class1
{
    //3.11
	public IPersonalizationXml GetAllWorkingLayers(long userId, int productId, string productTag, bool useMasterDB)
	{
        PersonalizationWebServiceResponse response = new PersonalizationWebServiceResponse();
        GetAllWorkingLayersRequest request = new GetAllWorkingLayersRequest(response, userId, productId, productTag, useMasterDB);
        SoapServiceDataProvider provider = new SoapServiceDataProvider();
        provider.Process(request);
        return new PersonalizationXml(response.Body);
	}
}

//3.12
public class GetAllWorkingLayersRequest : PersonalizationWebServiceResponse
{
    public GetAllWorkingLayersRequest(IConsumer consumer, long userId, int productId, string productTag, bool useMasterDB)
        : base(consumer, "PersonalizationService_GetAllWorkingLayers_1_0_Request")
    {
        this.AddElementToBody("userId", userId.ToString());
        this.AddElementToBody("productId", productId.ToString());
        this.AddElementToBody("productTag", productTag);
        this.AddElementToBody("useMasterDb", useMasterDb.ToString().ToLower());
    }

    // 3.14
    public void Consume(Stream stream, string encoding)
    {
        XmlTextReader xtr = new XmlTextReader(stream);
        xtr.WhitespaceHandling = WhitespaceHandling.None;
        try
        {
            while (xtr.Read())
            {
                // Ignore all nodes except Element nodes
                if (xtr.NodeType = XmlNodeType.Element)
                {
                    if (xtr.LocalName.Equals(SoapHeader))
                    {
                        xtr.Skip(); // skip the header
                    }
                    else if (xtr.LocalName != SoapEnvelope && xtr.LocalName != SoapBody)
                    {
                        if (multipleWorkingLayers)
                        {
                            dictionary = ReadWorkingLayerXml(userId, xtr);
                        }
                        else
                        {
                            component = new GroupComponent(null, null, false);
                            ReadXml(key, component, xtr);
                        }
                    }
                }
            }
        }
        finally
        {
            if (xtr != null)
            {
                xtr.Close();
            }
        }
        
        //3.15
        // Deserialize any properties into control
        if (properties != null && properties.Count > 0)
        {
            if (serializer == null)
            {
                NVOptions options = new NVOptions();
                options.ComplexKeys = true;
                serializer = NVPairSerializer.GetInstance(control.GetType(), options);
            }
            try
            {
                serializer.Deserialize(properties, control, new TemplateConverter(parent.Page));
            }
            catch(System.ArgumentException ae)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Error caused by trying to put the properties from component with id={0} into a HashTable.", this.Id);
                throw new ArgumentException(sb.ToString(), ae);
            }
        }
    }

    //3.16
    public override bool Inject(Control parent, RuleEvaluationContext ruleEvaluationContext)
    {
        Control control = null;
        if (type != null)
        {
            control = (System.Web.UI.Control)Activator.CreateInstance(type);
        }
        else
        {
            string themedSrc = scr;
            ResourcePath resolver = PathFactory.GetResourceResolver();
            if(resolver != null)
                themedSrc = resolver[themedSrc];
            try
            {
                control = parent.Page.LoadControl(themedSrc);
            }
            catch (Exception ex)
            {
                throw new CustomizationException(String.Format("Control component {0} src:{1} could not be loaded.", this.Id, src), ex);
            }
        }
        if (control != null)
        {
            //...
            // десиарилизация свойств компонента
            //...

            // Add to parent
            parent.Controls.Add(control);

            // Chain base injector - which injects children
            requiresBind = base.Inject(control, ruleEvaluationContext);
        }
        return requiresBind;
    }
}

//3.19

public interface IScreenerReportComponent
  {
    public ReportParametersCombined ReportParameters { get; set; } 
    public string ParameterProcessor { get; set; }
    public bool IsPersonalization { get; set; }
    public bool IsAdminMode { get; set; }

    protected override void OnInit(EventArgs e);
    protected override void ParseParameters();
    protected override void PrepareRequest();
    protected override object MakeSyncRequest();
    protected override void MakeAsyncRequest(EventHandler<Reuters.Express.Data.QueryCallBackEventArgs> callback);
    protected override void OnDataAvailable(object sender, Reuters.Express.Web.PageDataRegistryEventArgs e);
    public override void OnNoDataAvailable(NoDataAvailableEventArgs e);
  }